import * as api from '@/services/auth';
import { message } from 'antd';
import { getEnvURL } from '@/utils/utils';

export default {
  namespace: 'auth',

  state: {
    userInfo: {},
  },

  effects: {
    *sysLogin({ payload }, { call, put }) {
      let params = {};
      const result = yield call(api.sysLogin, payload);
      if (result.code && parseInt(result.code) == 200) {
        sessionStorage.setItem('userInfo', JSON.stringify(result.data));

        yield put({
          type: 'saveState',
          payload: {
            userInfo: result.data || {},
          },
        });
        const res = yield call(api.ticketLoginOuter, { ...result.data });
        sessionStorage.setItem('author', res.data.access_token);
        sessionStorage.setItem('tokenUUId', res.msg);
        if (res.code && parseInt(res.code) == 200) {
          const child = yield call(api.getRedirectInfor, {
            auth: res?.data.access_token,
            tokenUUId: res.msg,
          });
          params = {
            ...child,
            auth: res.data.access_token,
            tokenUUId: res.msg,
          };
        }
      }
      return params;
    },

    *loginOut({ payload }, { call }) {
      return yield call(api.loginOut);
      // const res = yield call(api.loginOut);
      // let result = null;
      // if (code && code == 200) {
      //     const res = yield call(api.clearTokenInfo, { param: data });
      //     result = res.code;
      // }
      // return result;
    },
  },

  reducers: {
    saveState(state, { payload }) {
      return { ...state, ...payload };
    },
  },
};
